from django.db import models
from django.conf import settings
from django.db.models import permalink
from django.core.files.storage import FileSystemStorage

import tagging
import capabilities.models

from endo.models import Endorsement
from t_ch.models import Technology

fs = FileSystemStorage(location=settings.STATIC_ROOT, base_url=settings.STATIC_URL)

class Category(models.Model):
    """
    A project category 
    """
    category = models.CharField(max_length=100, unique=True)
    slug = models.SlugField(unique=True)
    summary = models.TextField()
    order = capabilities.models.OrderField()
    class Meta:
        ordering = ['order']
    @permalink
    def get_absolute_url(self):
        return ('pr_j_category', [str(self.slug)])

    def __unicode__(self):
        return self.category

class Project(models.Model):
    """
    A project
    """
    author = capabilities.models.UserField()
    title = models.CharField(max_length=100)
    slug = models.SlugField(unique=True)
    client_name = models.CharField(max_length=100, blank=True)
    url = models.URLField(blank=True, null=True)
    launch_date = models.DateField(blank=True, null=True)
    short_summary = models.TextField()
    summary = models.TextField()
    body = models.TextField()
    published = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)
    order = capabilities.models.OrderField()
    featured = models.BooleanField(default=True)
    image = models.ImageField(storage=fs, upload_to='uploads/pr_j/project/image', blank=True, null=True)
    med_image = models.ImageField(storage=fs, upload_to='uploads/pr_j/project/med_image', blank=True, null=True)
    thumbnail = models.ImageField(storage=fs, upload_to='uploads/pr_j/project/thumbnail', blank=True, null=True)
    categories = models.ManyToManyField(Category)
    technologies = models.ManyToManyField(Technology)
    endorsements = models.ManyToManyField(Endorsement)
    
    class Meta:
        ordering = ['order']
        
    @permalink
    def get_absolute_url(self):
        return ('pr_j_project', [str(self.slug)])

    def __unicode__(self):
        return self.title

tagging.register(Project)

class Screenshot(models.Model):
    """
    A screenshot
    """
    project = models.ForeignKey(Project)
    screenshot = models.ImageField(upload_to='uploads/pr_j/screenshot/')
    order = capabilities.models.OrderField()
    
    class Meta:
        ordering = ['order']
        
    def __unicode__(self):
        return self.screenshot
